package com.example.comm.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.comm.Adapter.ChatAdapter;
import com.example.comm.Bean.Community;
import com.example.comm.Bean.User;
import com.example.comm.R;
import com.example.comm.activity.Pushcommunity;
import com.example.comm.activity.Pushcontent;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;

public class FragmentChat extends Fragment {

    private RecyclerView rv;
    private SwipeRefreshLayout srlayout;
    List<Community> data;
    private ChatAdapter chatAdapter;
    private FloatingActionButton add,addcontent,addcommunity;
    private RelativeLayout rvlayout;
    private View popview;

    private PopupWindow pop;
    //论坛界面

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragmentchat,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        popview=getLayoutInflater().inflate(R.layout.pop_item,null);
        initView(view);
        Refresh();

        //对button监听
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop=new PopupWindow(popview,300,500,true);
                pop.setOutsideTouchable(true);
                pop.setFocusable(true);
                pop.showAtLocation(rvlayout, Gravity.CENTER,530,300);

            }
        });

        addcontent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(getActivity(), "成功", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(getActivity(), Pushcontent.class));
            }
        });
        addcommunity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), Pushcommunity.class));
            }
        });
    }

    private void Refresh() {
        BmobQuery<Community> com=new BmobQuery<Community>();
        com.setLimit(1000);
        com.order("-createdAt");
        com.findObjects(new FindListener<Community>() {
            @Override
            public void done(List<Community> list, BmobException e) {
                srlayout.setRefreshing(false);
                if(e==null){
                    data=list;
                    chatAdapter=new ChatAdapter(getActivity(),data);
                    rv.setLayoutManager(new LinearLayoutManager(getActivity()));
                    rv.setAdapter(chatAdapter);
                }else {
                    srlayout.setRefreshing(false);
                    Toast.makeText(getActivity(), "加载失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initView(View view) {
        rv=view.findViewById(R.id.rv);
        srlayout=view.findViewById(R.id.swipe);
        add=view.findViewById(R.id.add);
        addcontent=popview.findViewById(R.id.addcontent);
        addcommunity=popview.findViewById(R.id.addcommunity);


        rvlayout=view.findViewById(R.id.rvlayout);

        srlayout.setColorSchemeResources(R.color.purple_200);
        //监听刷新
        srlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Refresh();
            }
        });


    }

}
